﻿
(function (window, undefined) {

    var $ = window.$, app = window.app;
    //保存表单
    window.app.saveForm = function (event, url) {
        var $button = $(event.currentTarget);
        var $form = $button.parent().prev(".modal-body").find("form"); //直接查找模态框的表单
        if ($form.length === 0) {
            $form = $("#modalcontent form");
        }
        if ($form.length === 0) {
            $form = $(".box-body form");
        }
        if ($form.length === 0) {
            window.app.message("怎么表单不对啊！");
            if (event && event.preventDefault) {
                //阻止默认浏览器动作(W3C)  
                event.preventDefault();
                event.stopPropagation();
            }
            return false;
        }


        if (false === $form.validate({ ignore: "" }).checkForm()) {
            if (event && event.preventDefault) {
                //阻止默认浏览器动作(W3C)  
                event.preventDefault();
                event.stopPropagation();
            }

            $form.valid();
            return false;
        }
        var form = new FormData($form[0]);
        $.ajax({
            type: "POST",
            url: url,
            data: form,
            processData: false,
            contentType: false,
            success: function (data) {
                if (data.errors.length > 0) {
                    swal("出错啦！", data.errors, "error");
                    $button.button('reset');
                } else {
                    if (data.redirectToUrl === null || data.redirectToUrl.length === 0) {
                        //关闭对话框刷新数据列表

                        window.app.modalClose($button[0]);
                        window.app.search($button[0]);

                        if (data.msg.length > 0) {
                            window.app.message(data.msg);
                        }
                    } else if (data.redirectToUrl.indexOf("javascript:") >= 0) {
                        eval(data.redirectToUrl.substr(10, data.redirectToUrl.length - 10));
                    } else if (data.redirectToUrl !== null && data.redirectToUrl.length > 0) {
                        //加载其他页面
                        window.app.modalClose();
                        window.app.loadUI(data.redirectToUrl);
                    }
                }
            },
            error: function (xmlHttpRequest, textStatus, errorThrown) {
                window.app.message("服务器异常。" + textStatus);
                $(event.target).button('reset');
            }
        });
    };
    //打开编辑页面
    window.app.showAddWindow = function (url, button, params, event) {
        var listControlId = $(button).data("search");
        if (typeof listControlId === "undefined") {
            listControlId = "";
        }
        var $dataTable = $(listControlId);
        if ($dataTable.length === 0) {
            window.app.message("实现错误，列表Id给错。");
            if (event && event.preventDefault) {
                //阻止默认浏览器动作(W3C)  
                event.preventDefault();
                event.stopPropagation();
            }
            return false;
        }
        url = url + "?ListControlId=" + escape(listControlId);
        for (var p in params) {
            var symbol = "?";
            if (url.indexOf("?") > -1)
                symbol = "&";

            url = url + symbol + p + "=" + escape(params[p]);
        }

        $(button).attr("href", url);
    };
    //打开编辑界面
    window.app.showEditWindow = function (url, button, params, event) {
        var listControlId = $(button).data("search");
        if (typeof listControlId === "undefined") {
            listControlId = "";
        }
        var $dataTable = $(listControlId);
        if ($dataTable.length === 0) {
            window.app.message("实现错误，列表Id给错。");
            if (event && event.preventDefault) {
                //阻止默认浏览器动作(W3C)  
                event.preventDefault();
                event.stopPropagation();
            }
            return false;
        }
        var ids = new Array();

        if ($dataTable.DataTable().rows()) {
            var list = $dataTable.DataTable().rows({ selected: true }).data();
            $.each(list,
                function (index, item) {
                    ids.push(item.id);
                    return true;
                });
        } else {
            $dataTable.find("tbody tr").each(function (x) {

                var checkbox = $(this).find(".checkColumn input");

                if (checkbox.length > 0 && checkbox.prop('checked')) {
                    ids.push(checkbox.val());
                    return true;
                }
            });
        }

        if (ids.length === 0) {
            window.app.message("请选中一项。");

            if (event && event.preventDefault) {
                //阻止默认浏览器动作(W3C)  
                event.preventDefault();
                event.stopPropagation();
            }
            return false;
        }
        if (ids.length > 1) {
            window.app.message("请只选中一项。");

            if (event && event.preventDefault) {
                //阻止默认浏览器动作(W3C)  
                event.preventDefault();
                event.stopPropagation();
            }

            return false;
        }


        params["Id"] = ids[0];
        params["ListControlId"] = listControlId;

        for (var p in params) {
            var symbol = "?";
            if (url.indexOf("?") > -1)
                symbol = "&";

            url = url + symbol + p + "=" + escape(params[p]);
        }

        $(button).attr("href", url);

        //if (params.IsReadOnlyForm)
        //    $(button).attr("href", $(button).attr("href") + "&isReadOnlyForm=true");

        return true;
    }

    //消息
    window.app.message = function (text) {
        swal({
            title: "",
            text: text,
            timer: 2000
        });
    };
    window.app.error = function (text) {
        sweetAlert("出错了...", text, "error");
    };

    //关闭窗口
    window.app.modalClose = function (button) {
        if (typeof button === "undefined") {

            $("#ajaxModal .modal").modal("hide");
            $("#ajaxModal").remove();
            $(".modal-backdrop").remove();
        } else {
            $(button).parents(".modal").modal("hide");

        }
    };

}(window));